package com.ruoyi.system.ali;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.common.utils.file.AliOss;
import com.ruoyi.system.domain.ZfbConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class AliPayService {

    @Resource
    private RedisCache redisCache;
    @Resource
    ISysConfigService configService;
    private static final String serverUrl = "https://openapi.alipay.com/gateway.do";


    public AliPayVo create(AliPayDto aliPayDto, ZfbConfig config) {
        String publicKey = config.getAlipayPublicKey();
        String privateKey = config.getPrivateKey();
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, config.getAppId(), privateKey, "json", "GBK", publicKey, "RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl(aliPayDto.getNotifyUtl());
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", aliPayDto.getOrderNo());
        bizContent.put("total_amount", aliPayDto.getAmount());
        bizContent.put("subject", "线下商品");
        //// 扩展信息，按需传入
        JSONObject extendParams = new JSONObject();
//        extendParams.put("sys_service_provider_id", "2088602262610814");
        bizContent.put("extend_params", extendParams);
        request.setBizContent(bizContent.toString());

        AlipayTradePrecreateResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (Exception e) {

            throw new BusiException("渠道发生错误");
        }

        if (!response.isSuccess()) {
            throw new BusiException(response.getSubMsg());
        }
        //将链接解析成二维码
        String qrCode = response.getQrCode();
        QrConfig qrConfig = new QrConfig();
        qrConfig.setWidth(200);
        qrConfig.setHeight(200);
        byte[] bytes = QrCodeUtil.generatePng(qrCode, qrConfig);
        String projectName = configService.selectConfigByKey("projectName") ;
        String picUrl = AliOss.uploadToOss(bytes, projectName + "/orderReceive/"+aliPayDto.getOrderNo() + ".jpg");
        Map<String, Object> map = new HashMap<>();
        //移动端支付宝跳转链接
        map.put("url", qrCode);
        map.put("pic", picUrl);
        map.put("amount", aliPayDto.getAmount());
        map.put("orderNo", aliPayDto.getOrderNo());
        AliPayVo aliPayVo = new AliPayVo();
        aliPayVo.setOrderNo(aliPayDto.getOrderNo());
        aliPayVo.setAmount(aliPayDto.getAmount());
        aliPayVo.setPic(picUrl);
        aliPayVo.setQrCode(qrCode);
        return aliPayVo;


    }


}
